<extend name="Public/base"/>

<block name="style">
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-table.css">
    <link rel="stylesheet" href="__STATIC__/styles/user_manage.css">
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-select.min.css">
    <link rel="stylesheet" href="__STATIC__/plugins/dropper/jquery.fs.dropper.min.css">
    <style type="text/css">
    label.error {
        color: red;
        position: absolute;
        top: 0px;
        left: 13px;
        filter: alpha(opacity=50);
        -moz-opacity: 0.5;
        -khtml-opacity: 0.5;
        opacity: 0.5;
    }
    .modal-body .control-group {
        position: relative;
    }   
    </style>
</block>

<block name="content">

    <div id="tree-container">
        <div id="tree"></div>
<eq name="uid" value="6">
        <div id="tree-tool" >
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 row">
                <select class="form-control" name="treeOption" id="treeOption">
                    <option value="1">新增</option>
                    <option value="2">修改</option>
                </select>
            </div>
            <div class="row">
                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" id="trash">
                    <span class="glyphicon glyphicon-trash" id="org_trash"></span>

                </div>
                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5" id="tree-add">
                    <input type="text" name="add_org_name" id="add_org_name" class="form-control">
                </div>
                <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                    <button type="button" class="btn btn-default" id="org_add">保存</button>
                </div>
            </div>

        </div>
</eq>
    </div>
   

<div id="table-container">
    <div class="bs-example bs-example-tabs nav-tabs-custom" data-example-id="togglable-tabs">
        <ul id="myTabs" class="nav nav-tabs" role="tablist">
            <li role="presentation" class="active"><a href="#webatve" id="webatve-tab" role="tab" data-toggle="tab" aria-controls="webatve" aria-expanded="true">web帐号信息</a></li>
            <li role="presentation" class=""><a href="#webatve" role="tab" id="appatve-tab" data-toggle="tab" aria-controls="webatve" aria-expanded="false">APP帐号信息</a></li>
        </ul>
        <div id="myTabContent" class="tab-content" style="max-height: 100%">
            <div role="tabpanel" class="tab-pane fade active in" id="webatve" aria-labelledby="webatve-tab">
                <div id="toolbar">
<!--                     <button id="remove" class="btn btn-danger btn-flat" disabled>
                        <i class="glyphicon glyphicon-remove"></i>删除
                    </button> -->
                    <button id="add" class="btn btn-primary btn-flat">
                        添加用户（设备）
                    </button>
                </div>
                <table id="table" data-toolbar="#toolbar">
                </table>
            </div>
        </div>
    </div>

</div>


    <div class="modal fade" id="complex_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form class="form-horizontal container-fluid"
                      id="editUser" method="post" action="{:U('editUser')}">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">
                        添加用户
                    </h4>
                </div>
                <div class="modal-body">
                    <div id="add_form_modal"></div>

                        <fieldset class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 label-container">
                                <div class="control-group"><label for="user_name">登录名</label></div>
                                <div class="control-group"><label for="real_name">姓名</label></div>
                                <div class="control-group"><label for="org_id">部门</label></div>
                                <div class="control-group"><label for="position">职务</label></div>
                                <div class="control-group"><label for="platform">平台</label></div>
                                <div class="control-group plat_about"><label for="role_id">角色</label></div>
                                <div class="control-group plat_mail"><label>所属群组</label></div>
                                

                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 modal-input">
                                <div class="control-group"><input type="text" class="form-control" id="user_name" name="user_name"></div>
                                <div class="control-group"><input type="text" class="form-control" id="real_name" name="real_name"></div>
                                <div class="control-group">
                                    <!--<div class="input-group " >-->
                                        <div class="input-group">
                                            <input type="hidden" id="org_id" name="org_id">
                                            <input type="text" id="org_name" name="org_name" class="form-control" value="">

                                        </div>
                                    
                                    <!--</div>-->

                                </div>
                                <div class="control-group"><input type="text" class="form-control" id="position" name="position"></div>
                                <div class="control-group">
                                    <select class="form-control" id="platform" name="platform_select">
                                        <volist name=":C('PLATFORM')" id="item">
                                            <option value="{$item.platform}">{$item.name}</option>
                                        </volist>
                    
                                    </select>
                                    <input type="text" readonly="readonly" name="show_platform" class="form-control"  id="show_platform">
                                </div>
                                <div class="control-group plat_about">
                                    <select class="form-control" id="role_id" name="role_id">

                                    </select>
                                </div>

                                <a href="javascript: void 0" id="accessControl" class="plat_about">角色和权限管理</a>
                                <div class="control-group plat_mail">
                                    <!--<div class="input-group " >-->
                                        <div class="input-group">
                                            <input type="hidden" id="group_code" name="group_code">
                                            <input type="text" id="group_name" name="group_name" class="form-control" value="" >

                                        </div>
                                    <!--</div>-->

                                </div>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 label-container">

                                <div class="control-group"><label for="password">密码</label></div>
                                <div class="control-group"><label  for="sex">性别</label></div>
                                <div class="control-group"><label for="phone">电话</label></div>
                                <div class="control-group"><label for="IDCard">身份证号</label></div>
                                <!--<div class="control-group"><label for="IMEI">设备ID</label></div>-->
                                <div class="control-group"><label>头像</label></div>

                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 modal-input">
                                <div class="control-group" ><input type="text" class="form-control" id="password" name="password"></div>
                                <div class="control-group">
                                    <select class=" form-control" id="sex" name="sex">
                                        <option value="1">男</option>
                                        <option value="2">女</option>
                                    </select>
                                </div>
                                <div class="control-group"><input type="text" class="form-control" id="phone" name="phone"></div>
                                <div class="control-group"><input type="text" class="form-control" id="idcard" name="IDCard"></div>
                                <!--<div class="control-group"><input type="text" class="form-control" id="IMEI" name="IMEI"></div>-->
                                <div class="control-group">
                                    <div class="controls">
                                        <div id="icon_div" class="col-sm-8" style="padding-left: 0px;">
                                            <input type="hidden" id="icon" name="icon" value="">

                                            <div class="image_show" style="display: none">

                                            </div>
                                            <div class="image_handle" data-toggle="tooltip" data-placement="top"
                                                 title=""
                                                 data-original-title="建议上传宽480px高320px的图片">
                                                <div class="dropped"></div>
                                            </div>
                                        </div>
                                        <div class="col-sm-5">
                                            <a href="javascript:void(0)" id="removeImg" class="btn btn-info invisible" style="margin-top: 10px;"
                                               role="button">删除图片</a>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </fieldset>

                </div>
                <input type="hidden" name="user_id" id="user_id" value="">
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary btn-flat" id="add_submit">确定</button>
                    <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">关闭</button>
                </div>
                </form>
            </div>
        </div>
    </div>

    <div class="modal fade right-not-show" id="access_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <div class="left">
                        <div id="role-table-container">
                            <div id="role_toolbar">
                                <button id="addRole" class="btn btn-primary btn-flat">新增角色</button>
                            </div>
                            <table id="role_table" data-toolbar="#role_toolbar"></table>
                        </div>
                    </div>
                    <div class="right">
                        <form id="roleForm" action="{:U('updateRoleAccess')}" method="post" role="form">
                            <h4>角色权限设置</h4>
                            <div class="control-group">
                                <label class="control-label" for="real_name">角色名称:</label>
                                <input type="text" class="form-control" id="role_name" name="role_name">
                            </div>
                            <div class="field-set">
                                <ul>
                                    <volist name="accessList" id="access">
                                        <li>
                                            <input type="checkbox" class="jurisdiction" name="jurisdiction[]" value="{$access.jurisdiction_id}"><label>{$access.name}</label>
                                        </li>
                                    </volist>
                                </ul>
                            </div>
                            <input type="hidden" name="role_id">
                            <button type="submit" id="editRoleAccess" class="btn btn-primary btn-flat" style="margin-right: 5px">确定</button>
                            <button type="button" class="btn btn-default btn-flat" id="access-right-close">关闭</button>
                        </form>
                    </div>
                    <div class="float-clear"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="confirm_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">确认</h4>
                </div>
                <div class="modal-body">
                    确定要删除吗?
                </div>
                <div class="modal-footer">
                    <button type="button" id="commonDel" class="btn btn-danger">删除</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="tree_group" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">所属群组</h4>
                </div>
                <div class="modal-body">
                    <div id="mini_tree_mail" style="display: block; overflow-y: auto; height: 400px;"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="tree_org" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">部门</h4>
                </div>
                <div class="modal-body">
                    <div id="mini_tree" style="display: block;overflow-y: auto; height: 400px;"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
</block>

<block name="script">

    <script src="__STATIC__/scripts/vendors/bootstrap-select.min.js"></script>
    <script src="__STATIC__/scripts/vendors/bootstrap-table.js"></script>
    <!-- <script src="__STATIC__/plugins/dropper/jquery.fs.dropper.min.js"></script> -->
    <script src="__STATIC__/scripts/vendors/bootstrap-treeview.min.js"></script>
    <script src="__STATIC__/plugins/dropper/jquery.fs.dropper.min.js"></script>
    <script src="__STATIC__/scripts/vendors/fileinput.min.js"></script>
    <script src="__STATIC__/scripts/vendors/jquery.form.min.js"></script>
    <script>
        var $table = $('#table');
        var $roleTable = $('#role_table');
        var $remove = $('#remove');
        var $tree = $('#tree');
        var $miniTree = $('#mini_tree');
        var $mini_tree_mail = $('#mini_tree_mail');
        var platform_status = 1;
        var org_id = "{$orgid}";
        var oTreeHelper,oMailTree, tableHelper, oMiniTreeHelper,oMiniMailTree, oRoleTableHelper;
        var $complexModal = $('#complex_modal');
        var $confirmModal = $('#confirm_modal');
        var $accessModal = $('#access_modal');
        var roleData = new Object();
        var orgData,mailData;
        var delUser = false;//是删除用户还是删除组织
        var $confirmDel = $('#commonDel');

        function initComplexModal(data){
            clearImageView();
            for(var p in data){
                if(typeof(data[p]) != "function"){
                    $complexModal.find('#' + p).val(data[p]);
                }
            }
//            if(data[idcard]){
//                $complexModal.find('#IDCard')
//            }
            if(data.org_id){
//                $miniTree.getNode(data.org_id);
            }

            if(data.icon){
                viewImageTointro(data.icon);
            }
            $('#show_platform').val($('#platform').find("option:selected").text());
            var platform = $('#platform').val();
//            var roleList = roleData[platform].roles;
            if(platform == 1){
                $('.plat_about').show();
                $('.plat_mail').show();
                
            }else{
                $('.plat_about').hide();
                $('.plat_mail').show();

            }
            $complexModal.find('.modal-title').text('编辑用户');
        }

        function resetRoleForm(){
            $form = $('#roleForm');
            $form[0].reset();
            $form.find('input:checkbox').each(function () {
                $(this).prop('checked', false);
            })
        }

        function initValidate(){
            $('#editUser').validate({
                debug:true,
                submitHandler:function (form) {
                    // var myreg = /^(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9})$/;
                    // if(!myreg.test($('#phone').val())){
                    //     return false;
                    // }
                    // if (isNaN($('#phone').val())) {
                    //     layer.tips('填写数字', '#phone', {
                    //       tips: [2, '#3595CC'],
                    //       time: 1500
                    //     });
                    //     return false;
                    // }
                    var url = $(form).prop('action');
                    var postData;
                    if ($('#org_id').val() === '' || $('#org_name').val() === '') {
                        layer.tips('请选择您的部门', '#org_name', {
                            tips: [3, '#3595CC'],
                            time: 1500
                        });
                        return false;
                    }
                    if ($('.plat_mail').css("display") == "block") {
                        if ($('#group_name').val() == '') {
                            layer.tips('所属群组必须选一个', '#group_name', {
                              tips: [3, '#3595CC'],
                              time: 1500
                            });
                            return false;
                        };
                        postData = $(form).serialize();
                        var show_platform = $('#show_platform').val();
                        if(show_platform == 'APP'){
                            $('#role_id').val(4);
                            postData += '&role_id=4';
                        }
                    }else{
                        $('#group_code').val("");
                        postData = $(form).serialize();
                    }
                    $.post(url,postData, function(data){
                        if(data.status == 1){
                            layer.msg('编辑成功');
                            form.reset();
                            clearImageView();
                            $complexModal.modal('hide');
                            $table.bootstrapTable('refresh');
                        }else{
                            layer.msg(data.msg);
                        }
                    });
                },
                rules:{
                    user_name:"required",
                    password:"required",
                    real_name:"required",
                },
                messages:{
                    user_name:"请填写登录名",
                    password:"请填写该用户密码",
                    real_name:'请填写姓名',
                }
            });

            $('#roleForm').validate({
                submitHandler:function(form){
                    var url = $(form).prop('action')
                    var postData = $(form).serialize();

                    $.post(url, postData, function(data){
                        if(data.status == 1){
                            oRoleTableHelper.refresh();
                            $accessModal.addClass('right-not-show');
                            resetRoleForm();
                        }else{
                            layer.msg(data.msg);
                        }

                    });
                },
                rules:{
                    role_name:'required',
                },
                messages:{
                    role_name:"角色名不能为空"
                }
            })
        }


        function initDropper(){
            $('#icon_div .dropped').dropper({
                postKey: "file",
                action: "{:U('File/uploadPicture')}",
                postData: {thumbSizes: '320x480', is_icon:1},
                label: "点击添加头像",
                maxSize: 1048576
            }).on("fileComplete.dropper", onFileComplete)
                    .on("fileError.dropper", onFileError);

            $('#removeImg').on("click",clearImageView );
        }

        function onFileComplete(e, file, response) {
            if(response.status == 0){
                layer.msg("抱歉上传失败");
            }else{
                viewImageTointro(response.info);
            }
        }

        function onFileError(e, file, error) {
            layer.msg(error);
        }

        function viewImageTointro(image) {
            if (image) {
                var root = "{:C('USER_ICON_URL')}";
                var imagesrc = root + image;
                $("#icon_div").find(".dropper-input").val("");
                $("#icon_div").find(".image_handle").hide();
                $("#icon_div").find(".image_show").show();
                $("#icon").val(image);
                $("#icon_div").find(".image_show").html("<img id='icon_preview' src='" + imagesrc + "' class='img-responsive' >");
                $("#removeImg").removeClass('invisible');
            }
        }

        function clearImageView () {
            var path_intro = $("#icon_preview").prop("src");
            $("#icon").val("");
            $("#icon_div").find(".image_show").html("");
            $("#icon_div").find(".image_handle").show();
            $("#icon_div").find(".dropper-input").val("");
            $("#removeImg").addClass('invisible');
        }

        function platfromChange(){
            // refreshRoleData();
            var platform = $('#platform').val();
            var roleList = roleData[1].roles;
            if(platform == 1){
                $('.plat_about').show();
                $('.plat_mail').show();
            }else{
                $('.plat_about').hide();
                $('.plat_mail').show();
            }
            $('#role_id').empty();
            for(var i in roleList){
                var html = "<option value='" + roleList[i].role_id + "'>"+ roleList[i].role_name + "</option>";
                $('#role_id').append(html);
            }

        }

        function refreshRoleData(){
            $.ajax({
                type:'post',
                url:"{:U('roleList')}",
                success:function(data){
                    if(data.status == 1){
                        roleData = data.data;
                        platfromChange();
                    }else{
                        layer.msg(data.msg);
                    }
                }
            })
        }
  // 修改开始 - 树形列表     
        var MailTree = function(){
            var oMailTree = new Object();

            oMailTree.init = function(data){
                mailData = data;
                if(oMiniMailTree){

                    oMiniMailTree.init(data);
                }
            }

            oMailTree.refresh= function(){
                $.ajax({
                    type:'post',
                    url:"{:U('getMail')}",
                    success:function(data){
                        if(data.status == 1){
                            oMailTree.init(data.data);
                        }else{
                            layer.msg(data.msg);
                        }
                    }
                });
            };

            return oMailTree;
        }

        var MiniMailTree = function(){
            var oMiniMailTree = new Object();

            oMiniMailTree.init = function(data){

                $mini_tree_mail.treeview({
                    data:data,
                    silent:true,
                    showBorder:false,
                    levels:1,
                    onNodeSelected:function (event, data) {
                        // if (data.group_code != '-1') {
                            $('#group_code').val(data.group_code);
                            $('#group_name').val(data.group_name);
                            var nodeId;
                            var obj = $mini_tree_mail.treeview('getSelected', nodeId);
                            $('#tree_group').modal('hide');
                        // }
                    }

                });
            };
            return oMiniMailTree;
        }

// 修改结束


        var TreeHelper = function(){
            var oTreeHelper = new Object();

            oTreeHelper.init = function(data){
                orgData = data;
                if(oMiniTreeHelper){
                    oMiniTreeHelper.init(data);
                }
                $tree.treeview({
                    data:data,
                    silent:true,
                    showBorder:false,
                    onNodeSelected:function (event, data) {
                        org_id = data.org_id;
                        if(tableHelper){
                            // tableHelper.refresh();

                            $table.bootstrapTable('destroy');
                            tableHelper.init();
                        }
                        if(org_id == 0){
                            $('#treeOption').val('1');
                        }

                    }

                });
            }

            oTreeHelper.refresh= function(){
                $.ajax({
                    type:'post',
                    url:"{:U('getOrgs')}",
                    success:function(data){
                        if(data.status == 1){
                            oTreeHelper.init(data.data);
                        }else{
                            layer.msg(data.msg);
                        }
                    }
                });
            };

            return oTreeHelper;
        }

        var MiniTreeHelper = function(){
            var oMiniTreeHelper = new Object();

            oMiniTreeHelper.init = function(data){
                $miniTree.treeview({
                    data:data,
                    silent:true,
                    showBorder:false,
                    onNodeSelected:function (event, data) {
                        if (data.org_id === 0) {
                            return false;
                        }
                        $('#org_id').val(data.org_id);
                        $('#org_name').val(data.org_name);
                        var nodeId;
                        var obj = $miniTree.treeview('getSelected', nodeId);
                        $('#tree_org').modal('hide');
                        // $miniTree.hide();
                    }

                });
            };
            return oMiniTreeHelper;
        }

        function conditconField(){
            if (platform_status == 2) {
                return 'group_name';
            }else{
                return 'role_name';
            }
        }
        function conditconTitle(){
            if (platform_status == 2) {
                return '通讯分组';
            }else{
                return '角色名称';
            }
        }
        var TableHelper = function(){
            var oTableHelper = new Object();

            oTableHelper.selections = [];


            oTableHelper.init = function(){
                $table.bootstrapTable({
                    formatLoadingMessage: function () {
                        return '正在努力地加载数据中，请稍候……';
                    },
                    formatRecordsPerPage: function (pageNumber) {
                        return '每页显示 ' + pageNumber + ' 条记录';
                    },
                    formatShowingRows: function (pageFrom, pageTo, totalRows) {
                        return '显示第 ' + pageFrom + ' 到第 ' + pageTo + ' 条记录，总共 ' + totalRows + ' 条记录';
                    },
                    formatSearch: function () {
                        return '姓名搜索';
                    },
                    formatNoMatches: function () {
                        return '没有找到匹配的记录';
                    },
                    formatPaginationSwitch: function () {
                        return '隐藏/显示分页';
                    },
                    formatRefresh: function () {
                        return '刷新';
                    },
                    formatToggle: function () {
                        return '切换';
                    },
                    formatColumns: function () {
                        return '列';
                    },
                    responseHandler:function(res){
                        return res;
                    },
                    url:"{:U('userList')}",
                    method:'get',
                    striped:true,
                    cache:false,
                    pagination:true,
                    sortable:false,
                    sortOrder:'asc',
                    sidePagination:'server',
                    queryParams:oTableHelper.mailListQueryParams,
                    pageNumber:1,
                    pageSize:10,
                    pageList:[10,20,50],
                    uniqueId:'user_id',
                    search:true,
                    height:getHeight(),

                    columns:[
//                         {
// //                            field: state,
//                             checkbox:true,
//                             align:'center'
//                         },
                        {
                            align: 'center',
                            field:'real_name',
                            title:'姓名',
                            formatter:function (value){
                                return '<a href="javascript: void 0" class="modify_user">' + value + '</a>';
                            },
                            events:operateEvents
                        },
                        {
                            align: 'center',
                            field:'sex',
                            title:'性别',
                            formatter:function(value){
                                if(value == 1){
                                    return '男';
                                }else{
                                    return '女';
                                }
                            }
                        },
                        {
                            align: 'center',
                            field:'user_name',
                            title:'登录名'
                        },
                        {
                            align: 'center',
                            field: conditconField(),
                            title: conditconTitle()
                        },
                        {
                            align: 'center',
                            field:'position',
                            title:'职务'
                        },
                        {
                            align: 'center',
                            field:'phone',
                            title:'电话'
                        },
                        {
                            align: 'center',
                            field:'state',
                            title:'状态',
                            formatter:function(value){
                                if(value==1){
                                    return '<span>可用&nbsp;</span><button class="btn btn-primary btn-flat changeState">冻结</button>';
                                }else{
                                    return '<span style="color:red">冻结&nbsp;</span><button class="btn btn-primary btn-flat changeState">解冻</button>';
                                }
                            },
                            events:operateEvents
                        },
                        {
                            field:'imei',
                            title:'设备解锁',
                            formatter:function (value){
                                if(value==''){
                                    return '设备已解锁';
                                }else{
                                    return '<button class="btn btn-primary btn-flat unlock">解锁 </button>';
                                }

                            }, 
                            align: 'center',
                            events:operateEvents
                        }


                    ]
                });


                $(window).resize(function () {
                    $table.bootstrapTable('resetView', {height:getHeight()});
                    $('#tree').css({
                        "height":getHeight()-30,
                        "overflow":"auto",
                    });
                });

                $table.on('check.bs.table uncheck.bs.table ' +
                    'check-all.bs.table uncheck-all.bs.table', function () {
                    $('#remove').prop('disabled', !$table.bootstrapTable('getSelections').length);

                    // save your data, here just save the current page
                    oTableHelper.selections = oTableHelper.getIdSelections();
                    // push or splice the selections if you want to save all data selections
                });
            };

            oTableHelper.mailListQueryParams = function(params){
                var temp = {
                    limit:params.limit,
                    offset:params.offset,
                    search:params.search,
                    org_id:org_id,
                    platform_status:platform_status
                };
                return temp;
            };


            oTableHelper.getIdSelections = function(){
                return $.map($table.bootstrapTable('getSelections'), function (row) {
                    return row.user_id;
                })
            };

            oTableHelper.refresh = function(){
                $table.bootstrapTable('refresh');
            };

            return oTableHelper;
        };

        var RoleTableHelper = function () {
            var oRoleTableHelper = new Object();
            oRoleTableHelper.init = function(){
                $roleTable.bootstrapTable({
                    formatLoadingMessage: function () {
                        return '正在努力地加载数据中，请稍候……';
                    },
                    formatRecordsPerPage: function (pageNumber) {
                        return '每页显示 ' + pageNumber + ' 条记录';
                    },
                    formatShowingRows: function (pageFrom, pageTo, totalRows) {
                        return '显示第 ' + pageFrom + ' 到第 ' + pageTo + ' 条记录，总共 ' + totalRows + ' 条记录';
                    },
                    formatSearch: function () {
                        return '搜索';
                    },
                    formatNoMatches: function () {
                        return '没有找到匹配的记录';
                    },
                    formatPaginationSwitch: function () {
                        return '隐藏/显示分页';
                    },
                    formatRefresh: function () {
                        return '刷新';
                    },
                    formatToggle: function () {
                        return '切换';
                    },
                    formatColumns: function () {
                        return '列';
                    },
                    responseHandler:function(res){
                        return res;
                    },
                    url:"{:U('roleTable')}",
                    method:'get',
                    striped:true,
                    cache:false,
                    pagination:true,
                    sortable:false,
                    sortOrder:'asc',
                    sidePagination:'server',
//                    queryParams:oRoleTableHelper.getQueryParams
                    pageNumber:1,
                    pageSize:5,
                    pageList:[5],
                    uniqueId:'role_id',
                    columns:[
                        {
                            field:'role_name',
                            title:'角色名称',
                        },
                        {
                            field:'jcount',
                            title:'已分配权限数'
                        },

                        {
                            title:'操作',
                            formatter:function(value, row){
                                return '<button class="btn btn-primary btn-flat role-modify">修改</button>';
                            },
                            events:operateEvents
                        }
                    ]
                });

            };

            oRoleTableHelper.refresh = function(){
                $roleTable.bootstrapTable('refresh');
            };

            return oRoleTableHelper;
        }





        function getHeight(){
            return $('#right').height() - 60;
        }

        window.operateEvents = {
            'click .modify_user':function(e, value, row, index){
                initComplexModal(row);
                $complexModal.modal('show');
            },
            'click .role-modify':function (e, value, row, index) {
                resetRoleForm();
                $accessModal.find('input[name=role_id]').val(row.role_id);
                $accessModal.find('#role_name').val(row.role_name);
                if(row.js){
                    for(var i in row.js){
                        var id = row.js[i];
                        var obj = $accessModal.find("input:checkbox[value="+id+"]");
                        obj.prop('checked', true);
                    }
                }
                $accessModal.removeClass('right-not-show');
            },
            'click .unlock':function(e, value, row, index){
                $.get(
                    "{:U('unlock')}",
                    {
                        user_id:row.user_id
                    },
                    function(data){
                        if(data.status == 1){
                            layer.msg('解锁成功');
                            $table.bootstrapTable('refresh');
                        }else{
                            layer.msg(data.msg);
                        }
                    }
                )
            },
            'click .changeState':function (e, value, row, index) {
                var state = row.state;
                if(state == 1){
                    state = 2;
                }else{
                    state = 1;
                }
                $.get(
                    "{:U('changeState')}",
                    {
                        user_id:row.user_id,
                        state:state
                    },
                    function(data){
                        if(data.status == 1){
                            layer.msg('操作成功');
                            $table.bootstrapTable('refresh');
                        }else{
                            layer.msg(data.msg);
                        }
                    }
                )
            }
        };



        $(function () {
            oTreeHelper = new TreeHelper();
            oMailTree    = new MailTree();
            tableHelper = new TableHelper();
            oMiniTreeHelper = new MiniTreeHelper();
            oMiniMailTree = new MiniMailTree();
            oRoleTableHelper = new RoleTableHelper();
            oMailTree.refresh();
            oTreeHelper.refresh();
            tableHelper.init();
            oRoleTableHelper.init();
            refreshRoleData();
            initDropper();
            initValidate();
            $('#add').click(function () {
                $('#editUser')[0].reset();
                $('.dropper-input').attr('name','dimg_input');
                $('#org_id').val('');
                clearImageView();
                $('#user_id').val('');
                platfromChange();
                if(platform_status == 1){
                    $('.plat_about').show();
                    $('.plat_mail').show();
                    $('#show_platform').val('web后台');
                }else{
                    $('.plat_about').hide();
                    $('.plat_mail').show();
                    $('#show_platform').val('APP');
                }
                $complexModal.modal('show');
            });
            $('#tree').css({
                "height":getHeight()-30,
                "overflow":"auto",
            });
            $('#platform').change(function () {
                platfromChange();
            });

            $('#accessControl').click(function () {
                $accessModal.modal('show');
            });

            $('#addRole').click(function(){
                $accessModal.find('input[name=role_id]').val('');
                $accessModal.removeClass('right-not-show');
            });

            $('#access-right-close').click(function () {
                $accessModal.addClass('right-not-show');
                resetRoleForm();
            });
            // 添加点击空白处因此树形节点
            // $(document).click(function(){
            //     $('#mini_tree').click(function(){
            //         return false;
            //     });
            //     $('#org_name').click(function(){
            //         return false;
            //     });
            //     if ($('#mini_tree').css('display') == 'block') {
            //         $('#mini_tree').hide();
            //     }
            // });
            $('#group_name').click(function(){
                $('#tree_group').modal('show');        
            });
            $('#org_name').click(function(){
                $('#tree_org').modal('show');
            });
            $('#myTabs li a').each(function(){
                $(this).click(function(){
                    if ($(this).attr('id') == 'appatve-tab') {
                        platform_status = 2;
                    }else{
                        platform_status = 1;
                    }
                    $table.bootstrapTable('destroy');
                    tableHelper.init();
                })
            });
            $('#platform').hide();
//            $('#editRoleAccess').click(function () {
//                var options = {
//                    url:$('#roleForm').prop('action'),
//                    type:'post',
//                    data:$('#roleForm').serialize(),
//                    success:function(data){
//                        if(data.status == 1){
//                            $accessModal.addClass('right-not-show');
//                            resetRoleForm();
//                        }else {
//                            layer.msg(data.msg);
//                        }
//                    }
//                };
//                $('#roleForm').ajaxForm(options);
//                return false;
//            });

            $('#org_trash').click(function () {
                if(org_id == 0){
                    return;
                }
                delUser = false;
                $confirmModal.modal('show');

            });

            $('#org_add').click(function () {
                var org_name = $('#tree-tool  #add_org_name').val();
                var selectOption= $('#treeOption').val();
                if(org_id == 0 && selectOption == 2 ){
                    layer.msg('不允许修改根节点');
                    return;
                }
                if(org_name && org_name != ''){
                    var url, params;
                    if(selectOption == 1){
                        url = "{:U('addOrg')}";
                        params = {
                            org_name:org_name,
                            parent_id:org_id
                        };
                    }else{
                        url = "{:U('editOrg')}";
                        params = {
                            org_name:org_name,
                            org_id:org_id
                        };
                    }
                    $.get(
                        url,
                        params,
                        function(data){
                            if(data.status == 1){
                                layer.msg('操作成功');
                                oTreeHelper.refresh();
                                $table.bootstrapTable('refresh');
                                $('#add_org_name').val('');
                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    )
                }else{
                    layer.msg('组织名不能为空');
                }
            });

            $remove.click(function(){
               delUser = true;
               $confirmModal.modal('show');
            });

            $confirmDel.click(function () {
                $confirmModal.modal('hide');
                if(delUser){
                    $.post({
                        url:"{:U('delete')}",
                        data:{
                            selectedIds : tableHelper.selections
                        },
                        success:function(data){
                            if(data.status == 1){
                                layer.msg('删除成功');
                                $table.bootstrapTable('refresh');
                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    })
                }else{
                    $.get(
                        "{:U('deleteOrg')}",
                        {
                            org_id:org_id,
                        },
                        function(data){
                            if(data.status == 1){
                                layer.msg('操作成功');
                                oTreeHelper.refresh();
                                $table.bootstrapTable('refresh');
                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    )
                }
            });


        })
    </script>

</block>